    //
//  FileViewController.m
//  studip
//
//  Created by anti on 05.07.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "FileViewController.h"


@implementation FileViewController

@synthesize courseId = _courseId;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)popToLauncherView
{
	[self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)loadView
{
	[super loadView];
	
	[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"eingangsbildschirm"] style:UIBarButtonItemStyleBordered target:self action:@selector(popToLauncherView)] autorelease] animated:YES];
}

- (id)initWithCourseId:(NSString*)courseId {
	// Note the [self init] here instead of [super init].
	if (self = [self init]) {
		self.courseId = courseId;
		self.tableViewStyle = UITableViewStyleGrouped;
		self.variableHeightRows = YES;
		
		CourseDummyData* dummyData = [CourseDummyData alloc];
		CourseModel* course = [CourseModel alloc];
		course.courseFiles = [dummyData getDummyFilesById:courseId];
		
		NSMutableArray* items = [[NSMutableArray  alloc] init];
		NSMutableArray* sections = [[NSMutableArray  alloc] init];
		NSMutableArray* itemRows = [[NSMutableArray  alloc] init];
		
		self.title = @"Files";
		
		NSArray* keys = [course.courseFiles allKeys];
		
		if ([keys count] > 0) {
			
			for (int i = 0; i < [keys count]; i++) {
		
				[sections addObject: [keys objectAtIndex: i]];
				NSMutableArray* itemRows = [[NSMutableArray  alloc] init];
				NSArray* files = [course.courseFiles objectForKey: [keys objectAtIndex: i]];
						
				if ([files count] > 0) {
					
					for (int j = 0; j < [files count]; j++) {
						NSMutableString* url = [[NSMutableString alloc] initWithString: @"fb://fileDetails/"];
						FileModel* file =  (FileModel*) [files objectAtIndex: j];
						[url appendString: file.fileId];
						
						
						[itemRows addObject:[TTTableTextItem
											 itemWithText: file.name
											 URL: url
											 accessoryURL: url]];
						
					}
					
				}
				else {
					[itemRows addObject:[TTTableTextItem
										 itemWithText: @"No files found in this folder"]];
					
				}

				[items addObject: itemRows];
				TT_RELEASE_SAFELY(itemRows);
			}
		}
		else {
			[itemRows addObject: [TTTableTextItem
								  itemWithText: @"No files found!"]];
		}

		TTSectionedDataSource* ds = [[TTSectionedDataSource alloc] autorelease];
		ds.sections = sections;
		ds.items = items;
		self.dataSource = ds;
		
		TT_RELEASE_SAFELY(sections);
		TT_RELEASE_SAFELY(items);
		
		
	}
	
	return self;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
